package fr.blogger.web.bean.administration.profil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.collections4.ListUtils;
import org.primefaces.model.DualListModel;

import fr.blogger.ejb.facade.ProfilFacade;
import fr.blogger.ejb.facade.RoleFacade;
import fr.blogger.ejb.model.Profil;
import fr.blogger.ejb.model.Role;
import fr.blogger.ejb.outils.enumeration.TypeModificationEnum;
import fr.blogger.web.outils.UtilsJsf;

@Named
@ViewScoped
public class AdministrationProfilBean implements Serializable {

	@Inject
	private ProfilFacade profilFacade;
	@Inject
	private RoleFacade roleFacade;
	private List<Profil> listeProfil = new ArrayList();
	private DualListModel<Role> listeRole;
	private Profil profil;
	private TypeModificationEnum typeModification;

	public AdministrationProfilBean() {
	}

	@PostConstruct
	private void initialisation() {
		rechercher();
	}

	private void rechercher() {
		listeProfil = profilFacade.findAll();
	}

	public void initNouveauProfil() {
		profil = new Profil();
		listeRole = new DualListModel(roleFacade.findAll(), new ArrayList());
		typeModification = TypeModificationEnum.creation;
		UtilsJsf.afficherDialog("_dialogModificationProfil");
	}

	public void selectionProfil() {
		typeModification = TypeModificationEnum.modification;
		listeRole = new DualListModel(ListUtils.removeAll(roleFacade.findAll(), profil.getListeRole()), profil.getListeRole());
		UtilsJsf.afficherDialog("_dialogModificationProfil");
	}

	public void valider() {
		profil = profilFacade.editerProfil(profil, listeRole.getTarget());
		UtilsJsf.ajoutMessageInfo("Le profil a bien été éditée");
		rechercher();
		UtilsJsf.cacherDialog("_dialogModificationProfil");
	}

	public List<Profil> getListeProfil() {
		return listeProfil;
	}

	public void setListeProfil(List<Profil> listeProfil) {
		this.listeProfil = listeProfil;
	}

	public DualListModel<Role> getListeRole() {
		return listeRole;
	}

	public void setListeRole(DualListModel<Role> listeRole) {
		this.listeRole = listeRole;
	}

	public Profil getProfil() {
		return profil;
	}

	public void setProfil(Profil profil) {
		this.profil = profil;
	}

	public TypeModificationEnum getTypeModification() {
		return typeModification;
	}

	public void setTypeModification(TypeModificationEnum typeModification) {
		this.typeModification = typeModification;
	}

}
